/**
 * Created by bestick on 2015/12/27.
 * 产品列表函数库
 */
$(document).ready(function () {

    //请求默认参数
    var comboURL = "/api/client/comb/getComb";
    var trustURL = "/api/client/tnonstandardproduct/showtnonstandarproductlist";
    var fundURL = "/api/client/standerproductfund/showstanderproductfundlist";
    var concernURL = "/api/client/concerned/addconcerned";
    var pList_render_domId = "#productList-detail";

    var nowPage = 1;
    var order = 0;
    var orderType = 0; //0降序1升序
    var pType = 1;
    var searchValue = '';
    var moneyMin = 4;
    var count = 10;
    var fundType = 1;

    //货币基金
    var currencyOrder = 1;
    var cFundType = 5;

    /**
     * 初始化slider
     */
    function initSlider() {
        //初始化slider
        $("input.slider").slider();
    }

    /**
     * 初始化收藏功能
     */
    function initConcerned() {
        //收藏product
        $('span.pList-detail-collect').click(function (e) {
            var $this = $(this);
            e.stopPropagation();
            doPost(
                concernURL,
                {"productId": $(this).parent().parent().parent().data('pid'), "t": now()},
                function (XMLHttpRequest) {
                    XMLHttpRequest.responseJSON.status != -1 ? $this.toggleClass('icon-collect-pitchon') : false;
                }
            );
        });
    }

    /**
     * 组合回调
     */
    function comboCallBack() {
        //初始化
        initConcerned();
        //进入组合详情
        $('.pList-detail-combo').click(function () {
            location.assign("/detail/productDetail/comboDetail?comboId=" + $(this).data("comboid"));
        });
    }

    /**
     * 信托回调
     */
    function trustCallBack() {
        //初始化
        initSlider();
        initConcerned();
        //进入信托详情
        $('.pList-detail-trust').click(function () {
            location.assign("/detail/productDetail/trustDetail?pId=" + $(this).data("pid"));
        });
    }

    /**
     * 基金回调
     */
    function fundCallBack() {
        //初始化
        initConcerned();
        //进入基金详情
        $('.pList-detail-fund').click(function () {
            location.assign("/detail/productDetail/fundDetail?pId=" + $(this).data("pid"));
        });
    }

    /**
     * 货币基金回调
     */
    function CFundCallBack() {
        //初始化
        initConcerned();
        //进入货币基金详情
        $('.pList-detail-fund').click(function () {
            location.assign("/detail/productDetail/cFundDetail?pId=" + $(this).data("pid"));
        });
    }

    /**
     * 组合渲染
     * @param callback 回调，必须
     * @param nowPage 当前页码
     */
    function comboRender(callback, nowPage) {
        doRender(
            comboURL,
            {
                "start": (nowPage - 1) * count,
                "length": count,
                "t": now()
            },
            {
                "#productList-combo-temp": pList_render_domId
            },
            function (XMLHttpRequest) {
                //获取total
                var total = eval('(' + XMLHttpRequest.responseText + ')').total;
                if (total >= 0) {
                    //在目标节点后append分页组件
                    $(pList_render_domId).append(genPagination(total, count, nowPage));
                    initPagination(callback, nowPage);
                }
                comboCallBack();
            }
        );
    }

    /**
     * 信托渲染
     * @param callback 回调，必须
     * @param nowPage 当前页码
     * @param order 排序方式类别，0预期收益率，1产品期限，2起购金额，5风险
     * @param orderType 升降序类别，0降序，1升序
     */
    function trustRender(callback, nowPage, order, orderType) {
        doRender(
            trustURL,
            {
                "order": order,
                "orderType": orderType,
                "pType": pType,
                "searchValue": searchValue,
                "moneyMin": moneyMin,
                "start": (nowPage - 1) * count,
                "count": count,
                "t": now()
            },
            {
                "#productList-trust-temp": pList_render_domId
            },
            function (XMLHttpRequest) {
                //获取total
                var total = eval('(' + XMLHttpRequest.responseText + ')').total;
                if (total >= 0) {
                    //在目标节点后append分页组件
                    $(pList_render_domId).append(genPagination(total, count, nowPage));
                    initPagination(callback, nowPage, order, orderType);
                }
                trustCallBack();
            }
        );
    }

    /**
     * 基金渲染
     * @param callback 回调，必须
     * @param nowPage 当前页码
     * @param order 排序方式类别，0费率，1单位净值，2日涨跌幅
     * @param orderType 升降序类别，0降序，1升序
     * @param fundType 基金类别，1股票，2债券，3混合，4指数，5货币，6其他
     */
    function fundRender(callback, nowPage, order, orderType, fundType) {
        doRender(
            fundURL,
            {
                "order": order,
                "orderType": orderType,
                "fundType": fundType,
                "searchValue": searchValue,
                "start": (nowPage - 1) * count,
                "count": count,
                "t": now()
            },
            {
                "#productList-fund-temp": pList_render_domId
            },
            function (XMLHttpRequest) {
                //获取total
                var total = eval('(' + XMLHttpRequest.responseText + ')').total;
                if (total >= 0) {
                    //在目标节点后append分页组件
                    $(pList_render_domId).append(genPagination(total, count, nowPage));
                    initPagination(callback, nowPage, order, orderType, fundType);
                }
                fundCallBack();
            }
        );
    }

    /**
     * 货币基金渲染
     * @param callback 回调，必须
     * @param nowPage 当前页码
     * @param order 排序方式类别，1万份收益，2七日年化
     * @param orderType 升降序类别，0降序，1升序
     */
    function cFundRender(callback, nowPage, order, orderType) {
        doRender(
            fundURL,
            {
                "order": order,
                "orderType": orderType,
                "fundType": cFundType,
                "searchValue": searchValue,
                "start": (nowPage - 1) * count,
                "count": count,
                "t": now()
            },
            {
                "#productList-cFund-temp": pList_render_domId
            },
            function (XMLHttpRequest) {
                //获取total
                var total = eval('(' + XMLHttpRequest.responseText + ')').total;
                if (total >= 0) {
                    //在目标节点后append分页组件
                    $(pList_render_domId).append(genPagination(total, count, nowPage));
                    initPagination(callback, nowPage, order, orderType);
                }
                CFundCallBack();
            }
        );
    }

    //信托排序渲染(风险order==5)
    function trustSortRender(obj) {
        if ($(obj).children().hasClass('glyphicon-arrow-down')) {
            $(obj).children().removeClass('glyphicon-arrow-down').addClass('glyphicon-arrow-up');
            trustRender(trustRender, nowPage, $(obj).index() == 0 ? 5 : $(obj).index() - 1, 1);
        } else if ($(obj).children().hasClass('glyphicon-arrow-up')) {
            $(obj).children().removeClass('glyphicon-arrow-up').addClass('glyphicon-arrow-down');
            trustRender(trustRender, nowPage, $(obj).index() == 0 ? 5 : $(obj).index() - 1, 0);
        } else {
            $(obj).children().addClass('glyphicon-arrow-down').parent().siblings().children().removeClass('glyphicon-arrow-down glyphicon-arrow-up');
            trustRender(trustRender, nowPage, $(obj).index() == 0 ? 5 : $(obj).index() - 1, 0);
        }
    }

    //基金排序渲染
    function fundSortRender(obj, fundType) {
        if ($(obj).children().hasClass('glyphicon-arrow-down')) {
            $(obj).children().removeClass('glyphicon-arrow-down').addClass('glyphicon-arrow-up');
            fundRender(fundRender, nowPage, $(obj).index(), 1, fundType);
        } else if ($(obj).children().hasClass('glyphicon-arrow-up')) {
            $(obj).children().removeClass('glyphicon-arrow-up').addClass('glyphicon-arrow-down');
            fundRender(fundRender, nowPage, $(obj).index(), 0, fundType);
        } else {
            $(obj).children().addClass('glyphicon-arrow-down').parent().siblings().children().removeClass('glyphicon-arrow-down glyphicon-arrow-up');
            fundRender(fundRender, nowPage, $(obj).index(), 0, fundType);
        }
    }

    //货币基金排序渲染
    function cFundSortRender(obj) {
        if ($(obj).children().hasClass('glyphicon-arrow-down')) {
            $(obj).children().removeClass('glyphicon-arrow-down').addClass('glyphicon-arrow-up');
            cFundRender(cFundRender, nowPage, $(obj).index() + 1, 1);
        } else if ($(obj).children().hasClass('glyphicon-arrow-up')) {
            $(obj).children().removeClass('glyphicon-arrow-up').addClass('glyphicon-arrow-down');
            cFundRender(cFundRender, nowPage, $(obj).index() + 1, 0);
        } else {
            $(obj).children().addClass('glyphicon-arrow-down').parent().siblings().children().removeClass('glyphicon-arrow-down glyphicon-arrow-up');
            cFundRender(cFundRender, nowPage, $(obj).index() + 1, 0);
        }
    }

    //切换productList-nav-tabs
    $('.productList-nav > ul > li').click(function () {
        $(this).addClass('productList-tabs-active').siblings().removeClass('productList-tabs-active');
        $('#tabs-fund').html('基金').next().removeClass('hidden');
        var $ul = $('.productList-subNav > ul:eq(' + $(this).index() + ')');
        //组合
        if ($(this).index() == 0) {
            $ul.siblings().hide();
            return;
        }
        //基金
        if ($(this).index() == 2) return;
        //货币、理财、资管、存款待定
        if ($(this).index() == 3 || $(this).index() == 4 || $(this).index() == 5 || $(this).index() == 6) return;
        $ul.find('li').eq(0).children().addClass('glyphicon-arrow-down').parent().siblings().children().removeClass('glyphicon-arrow-down glyphicon-arrow-up');
        $ul.slideDown("slow").siblings().hide();
    });

    //切换productList-trust-subTabs
    $('.productList-trust-subTabs > li').click(function () {
        trustSortRender(this);
    });

    //切换productList-fund-subTabs
    $('.productList-fund-subTabs > li').click(function () {
        fundSortRender(this, fundType);
    });

    //切换productList-cFund-subTabs（货币基金排序）
    $('.productList-cFund-subTabs > li').click(function() {
        cFundSortRender(this);
    });

    //点击productList-combo
    $('#productList-combo').click(function () {
        nowPage = 1;
        comboRender(comboRender, nowPage);
    });

    //点击productList-trust
    $('#productList-trust').click(function () {
        nowPage = 1;
        //默认按风险降序显示，order=5即风险
        trustRender(trustRender, nowPage, 5, orderType);
    });

    //点击productList-fund
    $('#productList-fund li').click(function (e) {
        e.stopPropagation();
        nowPage = 1;
        fundType = $(this).index() + 1;
        $('#productList-fund').removeClass('open');
        $('#tabs-fund').html($(this).find('span').html()).next().addClass('hidden');
        if ($(this).index() == 4) {
            $('.productList-cFund-subTabs').find('li').eq(0).children().addClass('glyphicon-arrow-down').parent().siblings().children().removeClass('glyphicon-arrow-down glyphicon-arrow-up');
            $('.productList-cFund-subTabs').slideDown("slow").siblings().hide();
            cFundRender(cFundRender, nowPage, currencyOrder, orderType);
            return;
        }
        $('.productList-fund-subTabs').find('li').eq(0).children().addClass('glyphicon-arrow-down').parent().siblings().children().removeClass('glyphicon-arrow-down glyphicon-arrow-up');
        $('.productList-fund-subTabs').slideDown("slow").siblings().hide();
        fundRender(fundRender, nowPage, order, orderType, fundType);
    });

    //TODO 点击productList-currency

    //TODO 点击productList-finance

    //TODO 点击productList-asset-management

    //TODO 点击productList-deposit

    //默认点击被激活的标签
    $('.productList-nav > ul > li').each(function () {
        if ($(this).hasClass('productList-tabs-active')) {
            $(this).click();
        }
    });

    //TODO 搜索
    $('#s_content').bind({
        "input propertychange": function () {}
    });
})